﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Valentia.Kinection.Configuration;

namespace Valentia.Kinection.Configuration.Manager
{
    public partial class frmConfigManager : Form
    {
        private ToolStripButton currentSelectedButton = null; RecordsGrid currentShowingGrid = null;
        private RecordsGrid kinectDevicedGrid, utilitiyUserGrid, serviceUsersGrid;
        private ServiceSettings settingControl;
        public static string currentSelectedControl = "Kinect Devices";
        public frmConfigManager()
        {
            InitializeComponent();
            lblFormHeading.Text = "Kinect Proxy Configuration :: Kinect Devices";
            this.kinectDevicedGrid =
                new RecordsGrid(typeof(KinectDevice), "ID",
                    new GridDispalyColumnInfo("KinectDeviceName", "Kinect Device Name"),
                    new GridDispalyColumnInfo("Active", "Active"),
                    new GridDispalyColumnInfo("KinectRuntimeOptionsString", "Kinect Runtime Options"),
                    new GridDispalyColumnInfo("GenerateVideoStream", "Generate Video Stream"),
                    new GridDispalyColumnInfo("GenerateDepthStream", "Generate Depth Stream"),
                    new GridDispalyColumnInfo("GenerateSkeletonStream", "Generate Skeleton Stream"));

            this.utilitiyUserGrid =
                    new RecordsGrid(typeof(UtilityUser), "ID",
                  new GridDispalyColumnInfo("Name", "Utility Client Name"),
                  new GridDispalyColumnInfo("Login", "Login"),
                  new GridDispalyColumnInfo("Active", "Active"));

            this.serviceUsersGrid =
                  new RecordsGrid(typeof(DataUser), "ID",
              new GridDispalyColumnInfo("Name", "Service Client Name"),
              new GridDispalyColumnInfo("Login", "Login"),
              new GridDispalyColumnInfo("Active", "Active"),
              new GridDispalyColumnInfo("SendVideoFrame", "Send Video Data"),
              new GridDispalyColumnInfo("SendDepthFrame", "Send Depth Data"),
              new GridDispalyColumnInfo("SendSkeletonFrame", "Send Skeleton Data"));

            this.kinectDevicedGrid.AddRecords(ConfigurationHelper.GetAllKinectDevices().ToArray());
            this.utilitiyUserGrid.AddRecords(ConfigurationHelper.GetAllUtilityUsers().ToArray());
            this.serviceUsersGrid.AddRecords(ConfigurationHelper.GetAllDataUsers().ToArray());

            this.settingControl = new ServiceSettings();

            this.RecordSelectionButton(this.btnKinectDevices);
        }

        private void frmConfigManager_Load(object sender, EventArgs e)
        {

        }

        private void AnyRecordsSelectionButton_Click(object sender, EventArgs e)
        {
            this.RecordSelectionButton(sender as ToolStripButton);
            currentSelectedControl = ((System.Windows.Forms.ToolStripItem)(sender)).ToolTipText.ToString();
            if (currentSelectedControl == "Kinect Devices")
            {
                lblFormHeading.Text = "Kinect Proxy Configuration :: Kinect Devices ";
            }
            else if (currentSelectedControl == "Utility Users")
            {
                lblFormHeading.Text = "Kinect Proxy Configuration :: Utility Users ";
            }
            else if (currentSelectedControl == "Service Users")
            {
                lblFormHeading.Text = "Kinect Proxy Configuration :: Service Users ";
            }
            else if (currentSelectedControl == "Service Settings")
            {
                lblFormHeading.Text = "Kinect Proxy Configuration :: Service Settings ";
            }
        }

        private void RecordSelectionButton(ToolStripButton button)
        {
            if (this.currentShowingGrid != null)
            {
                this.gbRecords.Controls.Remove(this.currentShowingGrid);
                this.currentShowingGrid = null;
            }

            if (this.gbRecords.Controls.Contains(this.settingControl)) { this.gbRecords.Controls.Remove(this.settingControl); }

            if (this.currentSelectedButton != null)
            {
                //  this.currentSelectedButton.Enabled = true;
                this.currentSelectedButton.Checked = false;
            }

            this.currentSelectedButton = button;
            //this.currentSelectedButton.Enabled = false;
            this.currentSelectedButton.Checked = true;


            if (Object.ReferenceEquals(button, this.btnServiceSettings))
            {
                //this.gbRecords.Text = "Service Settings";

                this.settingControl.LoadServiceSettings();
                this.settingControl.Dock = DockStyle.Fill;
                this.gbRecords.Controls.Add(this.settingControl);
            }
            else
            {
                //this.gbRecords.Text = "Records";

                if (Object.ReferenceEquals(button, this.btnKinectDevices))
                {
                    this.currentShowingGrid = this.kinectDevicedGrid;
                }
                else if (Object.ReferenceEquals(button, this.btnUtilityUsers))
                {
                    this.currentShowingGrid = this.utilitiyUserGrid;
                }
                else if (Object.ReferenceEquals(button, this.btnServiceUsers))
                {
                    this.currentShowingGrid = this.serviceUsersGrid;
                }

                this.currentShowingGrid.Dock = DockStyle.Fill;
                this.gbRecords.Controls.Add(this.currentShowingGrid);
            }
        }

        private void lvwMenu_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (!e.IsSelected)
                return;
        }

        private void lvwMenu_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}